#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/5/13 10:45
# @USER    : 717-3
# @File    : AddTwoNumbers.py
# @Software: PyCharm
# @Version  : Python-3.6
# @TASK:


class ListNode:
    """Definition for singly-linked list."""
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def addTwoNumbers(self, list1: ListNode, list2: ListNode):
        """
        You are given two non-empty linked lists representing two non-negative integers.
        The digits are stored in reverse order and each of their nodes contain a single digit.
        Add the two numbers and return it as a linked list.

        You may assume the two numbers do not contain any leading zero, except the number 0 itself.

        :param list1: ListNode
        :param list2: ListNode
        :return: ListNode

        Example:
            Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) \n
            Output: 7 -> 0 -> 8 \n
            Explanation: 342 + 465 = 807.
        """
        output = ListNode((list1.val + list2.val) % 10)
        resi = (list1.val + list2.val) // 10
        node = output
        while list1.next or list2.next:
            node.next = ListNode(((list1.next.val if list1.next else 0) +
                                  (list2.next.val if list2.next else 0) + resi) % 10)
            resi = ((list1.next.val if list1.next else 0) + (list2.next.val if list2.next else 0) + resi) // 10
            if list1.next:
                list1 = list1.next
            if list2.next:
                list2 = list2.next
            node = node.next
        if resi:
            node.next = ListNode(resi)
        return output


if __name__ == '__main__':
    # Q2
    S = Solution()
    input1 = [2, 4, 3]
    input2 = [5, 6, 4]
    print(input1)
    print(input2)
    l1 = ListNode(input1.pop(0))
    l2 = ListNode(input2.pop(0))
    temp1 = l1
    for element in input1:
        temp1.next = ListNode(element)
        temp1 = temp1.next
    temp2 = l2
    for element in input2:
        temp2.next = ListNode(element)
        temp2 = temp2.next
    root = S.addTwoNumbers(l1, l2)
    l3 = [root.val]
    while root.next:
        l3.append(root.next.val)
        root = root.next
    print(l3)
    print('done')
